jQuery(function ($) {
    $('.right-chart').css('margin-top',-266 + 'px');
    initSelectYear(4);
    let nowDate = new Date();
    let currentMonth = nowDate.getMonth() + 1, unms = $('#userNumMonthSelect');
    unms.append('<option>全部</option>');
    for (let g = 1; g <= currentMonth; g ++){
        unms.append('<option value="' + g + '">' + g + '月</option>');
    }
    $('#monthUserRegisterNumSelect').change(function () {
        initMonthUserRegisterNum();
    });
    $('#btnCountLoginUser').click(function () {
        initLoginOfUserNum();
    });
    initMonthUserRegisterNum();
    initIssueRobUserTotal();
    initLoginOfUserNum();
    initLevelUserNum();
    initSexUserNumPie();
    initHotCityUserNumPie();
    initClientUserNum();
});

/**
 * 初始化每个月用户注册数
 */
function initMonthUserRegisterNum(){
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listMonthUserRegisterNum?thisYear=' + $('#monthUserRegisterNumSelect').val(),
        dataType:'json',
        cache: true,
        success: function(murnData){
            drawMonthUserRegisterNum(murnData);
        }
    });
}
/**
 * 画initMonthUserRegisterNum（每个月用户注册数）折线图
 */
function drawMonthUserRegisterNum(murnData) {
    let muLegendArr = ['用户注册数'],monthArr = [], muSeriesArr = [],monthData = [];
    for (let g = 0; g < murnData.length; g ++){
        let obj = murnData[g];
        monthArr.push(obj.orderMonth + '月');
        monthData.push(obj.userNum);
    }
    muSeriesArr.push({
        name: '用户注册数',
        type: 'line',
        areaStyle: {},
        data: monthData
    });
    drawStackedLineChart('每个月的用户注册数',muLegendArr,monthArr,muSeriesArr,'monthUserChart',38,'个');
}

/**
 * 初始化从未下过单、未抢过单、抢过单未下过单、下过单未抢过单、既下单又抢单、下过单、抢过单等情况下的用户数量
 */
function initIssueRobUserTotal(){
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listAllUserAgeSection',
        dataType:'json',
        cache: true,
        success: function(ageSectionData){
            let ageUser = ageSectionData[0],irUserArr = [];
            for (let f in ageUser) {
                irUserArr.push({name: f,value: ageUser[f]})
            }
            drawCommonPie('所有用户年龄情况','用户数及占比',irUserArr,'issueRobUserCountPie');
        }
    });
}
/**
 * 初始化微信/手机等用户类型的数量数据
 */
function initClientUserNum(){
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listClientUserNum',
        dataType:'json',
        cache: true,
        success: function(clientUserData){
            drawCommonPie('用户来源数量','用户数及占比',clientUserData,'clientUserPie');
        }
    });
}

/**
 * 初始化某年/某月的登录的用户数
 */
function initLoginOfUserNum() {
    let y = $('#userNumYearSelect').val(), m = $('#userNumMonthSelect').val();
    if (m == '全部'){
        m = '';
    }
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listLoginOfUserNum?thisYear=' + y + '&thisMonth=' + m,
        dataType:'json',
        cache: true,
        success: function(userNumData){
            drawLoginOfUserNum(userNumData,y,m);
        }
    });
}

/**
 * 画initLoginOfUserNum（某年/某月的登录的用户数）折线图
 * @param userNumData 登录的用户数
 */
function drawLoginOfUserNum(userNumData,countYear,countMonth) {
    let muLegendArr = ['登录用户数'],monthArr = [], muSeriesArr = [],monthData = [];
    for (let g = 0; g < userNumData.length; g ++){
        let obj = userNumData[g];
        monthArr.push(obj.mo + (isNull(countMonth) ? '月' : '日'));
        monthData.push(obj.loginUserNum);
    }
    muSeriesArr.push({
        name: '登录用户数',
        type: 'line',
        areaStyle: {},
        data: monthData
    });
    let title = '  ' + countYear + '年';
    if (!isNull(countMonth)){
        title += countMonth + '月';
    }
    drawStackedLineChart(title + '的登录用户数',muLegendArr,monthArr,muSeriesArr,'loginOfUserNum',38,'个');
}

/**
 * 初始化各等级的用户数量及占比
 */
function initLevelUserNum(){
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listLevelUserNum',
        dataType:'json',
        cache: true,
        success: function(levelData){
            drawLevelUserNum(levelData);
        }
    });
}
/**
 * 画initLevelUserNum（各等级的用户数量及占比）函数的折线图
 */
function drawLevelUserNum(levelData) {
    let levelArr = [1,2,3,4,5],xAxisArr = [], seriesArr = [];
    for (let g = 0; g < levelArr.length; g ++){
        let l = levelArr[g],existsLevel = false;
        xAxisArr.push(g + 1 + '星');
        for (let i = 0; i < levelData.length; i ++){
            let lObj = levelData[i];
            if (l == lObj.grade){
                existsLevel = true;
                seriesArr.push(lObj.gradeUserNum);
            }
        }
        if (!existsLevel){
            seriesArr.push(0);
        }
    }
    let levelOption = getSimpleBarOption('各等级的用户量',xAxisArr,seriesArr,'个');
    setChartOption(levelOption,'levelUserNumBar');
}
/**
 * 初始化用户男女的数量
 */
function initSexUserNumPie(){
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listSexUserNum',
        dataType:'json',
        cache: true,
        success: function(sexUserData){
            drawCommonPie('用户男女数量及占比','男女总量(占比)',sexUserData,'sexUserNumPie');
        }
    });
}
/**
 * 初始化热门城市用户的数量
 */
function initHotCityUserNumPie(){
    $.ajax({
        type: "POST",
        url: '/userInfoChart/listHotCityUserNum',
        dataType:'json',
        cache: true,
        success: function(hotCityUserData){
            drawCommonPie('所在热门城市用户量及占比','城市用户量(占比)',hotCityUserData,'hotCityUserNumPie');
        }
    });
}